package utils

import (
	"context"
	"os/exec"
	"runtime"
	"time"
)

// OpenBrowser 自动打开浏览器
func OpenBrowser(url string) error {
	var cmd string
	var args []string

	switch runtime.GOOS {
	case "darwin": // macOS
		cmd = "open"
		args = []string{"-a", "Google Chrome", url}
	case "windows":
		cmd = "cmd"
		args = []string{"/c", "start", "", url}
	case "linux":
		cmd = "xdg-open"
		args = []string{url}
	default:
		cmd = "xdg-open"
		args = []string{url}
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	return exec.CommandContext(ctx, cmd, args...).Start()
}
